package dao

import (
	"fmt"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/wangyuelog"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

// -------------------- dao层 - 扫码日志相关操作 -----------------------//

type WyLogDao struct {
}

func NewWyLogDao() *WyLogDao {
	return &WyLogDao{}
}

// InsertWyLog 新增扫码日志
func (d *WyLogDao) InsertWyLog(m *wangyuelog.WyLogModel) *dingo.ErrorWrap {

	db := global.Orm.Table("wy_log").Create(m)
	return dingo.ErrorIf(db.Error, true)
}

// GetWyLogList 预约按摩日志
func (d *WyLogDao) GetWyLogList(param *request.SpaListReq) *dingo.ErrorWrap {
	var (
		lst    []wangyuelog.SpaLogList
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)
	field := `wl.*,p.province,p.city,p.county,p.address,REPLACE(m.phone, SUBSTR(phone,4,4), '****') as phone`
	db := global.Orm.Table("wy_log wl").
		Select(field).
		Joins("LEFT JOIN places p ON wl.place_id = p.id").
		Joins("LEFT JOIN members m ON wl.member_id = m.id")
	if param.PlaceName != "" { db.Where("wl.place_name like ?", fmt.Sprint("%", param.PlaceName, "%")) }
	if param.PlaceId > 0 { db.Where("wl.place_id = ?", param.PlaceId) }
	if param.LogType != "" { db.Where("wl.log_type = ?", param.LogType) }
	db.Order("wl.id desc")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}
